﻿Public Class CommonMethods

    Public Structure ComboBoxItem
        Dim Key As Object
        Dim Value As Object

        Public Sub New(pKey As Object, pValue As Object)
            Me.Key = pKey
            Me.Value = pValue
        End Sub
    End Structure

    Public Enum SqlDataType
        DataType_String
        DataType_Integer
    End Enum

    Public Shared Function ToSqlStr(pString As Object, pDataType As SqlDataType, Optional pHasQuote As Boolean = True)
        Dim xRet As String

        If IsDBNull(pString) Or pString Is Nothing Then
            Return "NULL"
        End If

        Select Case pDataType
            Case SqlDataType.DataType_Integer
                xRet = CStr(pString)
            Case SqlDataType.DataType_String
                If pHasQuote = True Then
                    xRet = "N'" & Replace(CStr(pString), "'", "''") & "'"
                Else
                    xRet = Replace(CStr(pString), "'", "''")
                End If
            Case Else
                xRet = "N'" & Replace(CStr(pString), "'", "''") & "'"
        End Select

        Return xRet
    End Function

    Public Shared Function fToText(pObj As Object) As String
        Dim xRet As String

        If IsDBNull(pObj) = False Then
            xRet = CStr(pObj)
        Else
            xRet = ""
        End If

        Return xRet
    End Function

    Public Shared Function fToInt(pObj As Object) As Integer
        Dim nRet As Integer

        If IsDBNull(pObj) = False Then
            nRet = CInt(pObj)
        Else
            nRet = 0
        End If

        Return nRet
    End Function

    ''' <summary>
    ''' Bỏ dấu tiếng việt
    ''' </summary>
    ''' <param name="pString"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function TrimAccentedString(pString As String) As String
        Dim v_reg_regex As New Regex("\p{IsCombiningDiacriticalMarks}+")
        Dim v_str_FormD As String = pString.Normalize(NormalizationForm.FormD)
        Return v_reg_regex.Replace(v_str_FormD, [String].Empty).Replace("đ"c, "d"c).Replace("Đ"c, "D"c)
    End Function

    ''' <summary>
    ''' Thay thế các khoảng trắng liên tiếp thành 1 khoảng trắng
    ''' </summary>
    ''' <param name="pString"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Shared Function TrimMultiWhiteSpace(pString As String) As String
        Dim regex As New Regex("\s+")
        Return regex.Replace(pString, " ")
    End Function

    Public Shared Function TrimNonAlphaNumeric(pString As String) As String
        Dim regex As New Regex("[^a-zA-Z0-9 ]")
        Return regex.Replace(pString, "")
    End Function

    Public Shared Function Nz(pPar1 As Object, pPar2 As Object) As Object

        If IsNothing(pPar1) = True OrElse IsDBNull(pPar1) = True Then
            Return pPar2
        Else
            Return pPar1
        End If

    End Function
End Class
